package com.dhh.activity;

import com.dhh.R;
import com.dhh.dao.DataBaseHelper;
import com.dhh.util.Constant;
import com.dhh.util.manager.FoodManager;
import com.dhh.vo.Food;

import greendroid.app.GDActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.text.method.DigitsKeyListener;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AddCustomFoodActivity extends GDActivity implements View.OnClickListener {
	private DataBaseHelper db;
	private FoodManager fManager;
	private LinearLayout lFoodName;
	private LinearLayout lFoodGroup;
	private LinearLayout lCarb;
	private LinearLayout lProtein;
	private LinearLayout lFat;
	private TextView foodName;
	private TextView foodGroup;
	private TextView carb;
	private TextView protein;
	private TextView fat;
	private TextView energy;
	private Button sButton;
	private Food food;
	
	@Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setActionBarContentView(R.layout.form);   
		
        db = new DataBaseHelper(getBaseContext());
        db.openDataBase();        
        fManager = new FoodManager(db);
		food = new Food();
		
        initActionBar();
	}
	
	@Override
	public void onStart(){
		super.onStart();
        initView();
        fillData();
	}
	
	@Override
    public void onResume(){
    	super.onResume();
        db.openDataBase();       
    }
    
    @Override
	protected void onPause(){
		super.onPause();
		if (db != null) {
			db.close();
		}
	}
    
    @Override
	protected void onStop(){
		super.onStop();
	}
	
	public void initView(){
		foodName = (TextView) this.findViewById(R.id.ac_food_name);
		foodGroup = (TextView) this.findViewById(R.id.ac_food_group);
		fat = (TextView) this.findViewById(R.id.ac_fat);
		protein = (TextView) this.findViewById(R.id.ac_protein);
		carb = (TextView) this.findViewById(R.id.ac_carb);
		energy = (TextView) this.findViewById(R.id.ac_calorie);
		
		lFoodName = (LinearLayout) this.findViewById(R.id.ac_lfoodname);
		lFoodGroup = (LinearLayout) this.findViewById(R.id.ac_lfoodgroup);
		lFat = (LinearLayout) this.findViewById(R.id.ac_lfat);
		lProtein = (LinearLayout) this.findViewById(R.id.ac_lprotein);
		lCarb = (LinearLayout) this.findViewById(R.id.ac_lcarb);
		
		sButton = (Button) this.findViewById(R.id.ac_add);
		sButton.setOnClickListener(this);
		
		lFoodName.setOnClickListener(this);
		lFoodGroup.setOnClickListener(this);
		lFat.setOnClickListener(this);
		lProtein.setOnClickListener(this);
		lCarb.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {		
		if(v.getId() == R.id.ac_lfoodgroup){
			getAlertDialogChoice("","",Constant.FOOD_GROUPS).show();
		}else if(v.getId() == R.id.ac_add){
			fManager.addFood(food);
			Intent intent = new Intent(AddCustomFoodActivity.this,FoodAddActivity.class);
			this.startActivity(intent);
		}else{
			getAlertDialogTextEdit("","",v.getId()).show();
		}
	}
	
	private void initActionBar(){
		getActionBar().setTitle("Create Your Own Food");
        getActionBar().setBackgroundColor(Color.rgb(14, 166, 249));
        getActionBar().getChildAt(0).
        	setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(AddCustomFoodActivity.this,HomeActivity.class);	
					startActivity(intent);
				}});
	}
	
	private void fillData(){
		foodName.setText(food.getFoodName()!=null?food.getFoodName():"");
		foodGroup.setText(food.getFoodGroup()!=null?food.getFoodGroup():"");
		fat.setText(food.getFat()!=null?food.getFat()+" g":"");
		protein.setText(food.getProtein()!=null?food.getProtein()+" g":"");
		carb.setText(food.getCarb()!=null?food.getCarb()+" g":"");
		energy.setText(food.getEnergy());
		
		checkShowButton();
	}
	
	private AlertDialog getAlertDialogTextEdit(String title,String defaultValue,final int id){
		final FrameLayout fl = new FrameLayout(AddCustomFoodActivity.this);
		final EditText input = new EditText(AddCustomFoodActivity.this);
		input.setGravity(Gravity.CENTER);
		fl.addView(input, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
		input.setText(defaultValue);
		if(id==R.id.ac_lfat || id==R.id.ac_lprotein || id==R.id.ac_lcarb){
			input.setInputType(InputType.TYPE_CLASS_NUMBER);
			DigitsKeyListener digkl = DigitsKeyListener.getInstance(false,true);
			input.setKeyListener(digkl);
		}
			
		
		AlertDialog.Builder builder = new AlertDialog.Builder(AddCustomFoodActivity.this);
		builder.setView(fl).setTitle(title).
			setPositiveButton("OK", new DialogInterface.OnClickListener(){		    	 
                @Override
                public void onClick(DialogInterface d, int which) {
                	if(R.id.ac_lfoodname == id){
            			food.setFoodName(input.getText().toString());
            		}else if(R.id.ac_lfoodgroup == id){
            			food.setFoodGroup(input.getText().toString());
            		}else if(R.id.ac_lfat == id){
            			food.setFat(input.getText().toString());
            		}else if(R.id.ac_lprotein == id){
            			food.setProtein(input.getText().toString());
            		}else if(R.id.ac_lcarb == id){
            			food.setCarb(input.getText().toString());
            		}
        			fillData();
                	d.dismiss();
                }
		    }).
		    setNegativeButton("Cancel", new DialogInterface.OnClickListener(){		    	 
                @Override
                public void onClick(DialogInterface d, int which) {
                	d.dismiss();
                }
		    });
		
		return builder.create();
	}
	
	int selectedItem = 0;
	AlertDialog getAlertDialogChoice(String title,String value,final String items[]){
		AlertDialog.Builder ab = new AlertDialog.Builder(AddCustomFoodActivity.this);
		ab.setTitle(title);		
		ab.setSingleChoiceItems(items, selectedItem,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						selectedItem = whichButton;
					}
				})
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						food.setFoodGroup(items[selectedItem]);	
						fillData();
					}
				})
				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,int whichButton) {
								dialog.dismiss();
							}
						});
		return ab.create();
	}
	
	public void checkShowButton(){
		if(food.getFoodName()!=null && 
				food.getFat()!=null &&
				food.getFoodGroup()!=null &&
				food.getProtein()!=null &&
				food.getCarb()!=null){
			sButton.setVisibility(View.VISIBLE);
		}else{
			sButton.setVisibility(View.INVISIBLE);
		}
	}

}
